package com.bysj.sausys.service.impl;

import com.bysj.sausys.beans.PageQuery;
import com.bysj.sausys.beans.PageResult;
import com.bysj.sausys.beans.Pagination;
import com.bysj.sausys.common.AssConst;
import com.bysj.sausys.dao.SysAssMapper;
import com.bysj.sausys.exception.ParamException;
import com.bysj.sausys.model.SysAss;
import com.bysj.sausys.service.ISysAssService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社团service接口实现
 *
 * @author WangZK
 */
@Service("sysAssService")
public class SysAssServiceImpl implements ISysAssService {

    @Resource
    private SysAssMapper sysAssMapper;

    /**
     * 保存社团信息
     *
     * @param sysAss
     * @param username
     */
    @Override
    public void save(SysAss sysAss, String username) {
        if (checkAssExist(sysAss.getId(), sysAss.getName())) {
            throw new ParamException("社团已存在，新增失败");
        }
        sysAss.setInsertMan(username);
        sysAss.setInsertTime(new Date());

        sysAssMapper.insertSelective(sysAss);
    }

    /**
     * 判断社团是否已存在
     *
     * @param id
     * @param name
     * @return
     */
    private boolean checkAssExist(Integer id, String name) {
        return sysAssMapper.countByName(id, name) > 0;
    }

    /**
     * 更新社团信息
     *
     * @param sysAss
     * @param username
     */
    @Override
    public void update(SysAss sysAss, String username) {
        SysAss before = sysAssMapper.selectByPrimaryKey(sysAss.getId());
        if (before == null) {
            throw new ParamException("待更新的社团不存在");
        }
        if (checkAssExist(sysAss.getId(), sysAss.getName())) {
            throw new ParamException("社团已存在，更新失败");
        }

        SysAss after = new SysAss();

        after.setId(sysAss.getId());
        after.setName(sysAss.getName());
        after.setCategory(sysAss.getCategory());
        after.setPurpose(sysAss.getPurpose());
        after.setActField(sysAss.getActField());
        after.setAssFilename(sysAss.getAssFilename());
        after.setAssFile(sysAss.getAssFile());
        after.setInitSituation(sysAss.getInitSituation());
        after.setLeadSituation(sysAss.getLeadSituation());
        after.setLeadTeacherSituation(sysAss.getLeadTeacherSituation());
        after.setRemarks(sysAss.getRemarks());
        after.setStatus(before.getStatus());
        after.setLastupdMan(username);
        after.setLastupdTime(new Date());

        sysAssMapper.updateByPrimaryKeySelective(after);
    }


    /**
     * 根据id修改社团状态
     *
     * @param assId
     * @param username
     * @return
     */
    @Override
    public int updateStatus(Integer assId, String username) {
        Map params = new HashMap(4);
        params.put("status", AssConst.CANCEL.getCode());
        params.put("lastupdTime", new Date());
        params.put("lastupdMan", username);
        return sysAssMapper.updateStatusByPrimaryKey(assId, params);
    }

    /**
     * 获取社团列表
     *
     * @param keyword
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult getPageByKeyword(String keyword, PageQuery pageQuery) {
        int count = sysAssMapper.countByKeyword(keyword);
        if (count > 0) {
            List<SysAss> sysAssList = sysAssMapper.getPageByKeyword(keyword, pageQuery);
            PageResult<SysAss> result = new PageResult<>();
            result.setList(sysAssList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }

    /**
     * 根据id获取社团信息
     *
     * @param id
     * @return
     */
    @Override
    public SysAss findById(Integer id) {
        SysAss sysAss = sysAssMapper.selectByPrimaryKey(id);
        if (sysAss == null) {
            throw new ParamException("获取社团信息失败");
        }
        return sysAss;
    }

    /**
     * 获取所有社团编号和名称
     *
     * @param status
     * @return
     */
    @Override
    public List<Map> getAllIdAndNameAndStatus(Integer status) {
        return sysAssMapper.getAllIdAndNameAndStatus(status);
    }

    /**
     * 根据社团状态和社团类别分页获取社团列表
     *
     * @param type
     * @param status
     * @param pageQuery
     * @return
     */
    @Override
    public PageResult getPageByTypeAndStatus(String type, Integer status, PageQuery pageQuery) {
        int count = sysAssMapper.countByTypeAndStatus(type, status);
        if (count > 0) {
            List<SysAss> sysAssList = sysAssMapper.getPageByTypeAndStatus(type, status, pageQuery);
            PageResult<SysAss> result = new PageResult<>();
            result.setList(sysAssList);
            Pagination pagination = new Pagination();
            pagination.setTotal(count);
            pagination.setPageSize(pageQuery.getPageSize());
            pagination.setCurrentPage(pageQuery.getPageNo());
            result.setPagination(pagination);
            return result;
        }
        return new PageResult<>();
    }
}
